import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.filechooser.FileNameExtensionFilter;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import org.xml.sax.SAXException;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import java.awt.Font;


public class AddEditSubject extends JDialog {

	private final JPanel contentPanel = new JPanel();

	/**
	 * Create the dialog.
	 */
	public AddEditSubject() {
		setBounds(100, 100, 304, 405);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		
		final JComboBox comboBox = new JComboBox();
		comboBox.setBounds(10, 27, 142, 22);
		
		File subjectDirectory = new File ("subjects");
		File[] subjects = subjectDirectory.listFiles();
		for (int i = 0; i < subjects.length; i++){
			comboBox.addItem(subjects[i].getName());
		}
		
		contentPanel.add(comboBox);
		
		{
			JButton btnNewButton = new JButton("Add Subject");
			btnNewButton.setFont(new Font("Tahoma", Font.PLAIN, 10));
			btnNewButton.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent arg0) {
					AddSubjectDialog dialog = new AddSubjectDialog (comboBox);
				}
			});
			btnNewButton.setBounds(10, 60, 116, 23);
			contentPanel.add(btnNewButton);
		}
		
		JLabel lblSubject = new JLabel("Subject:");
		lblSubject.setBounds(10, 11, 46, 14);
		contentPanel.add(lblSubject);
		
		JLabel lblTestAssignedTo = new JLabel("Tests Assigned to Subject:");
		lblTestAssignedTo.setBounds(10, 94, 215, 14);
		contentPanel.add(lblTestAssignedTo);
		
		final JList list = new JList();
		list.setBounds(10, 117, 268, 179);
		contentPanel.add(list);
		
		JButton btnAssignTest = new JButton("Assign Test");
		btnAssignTest.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				JFileChooser assignDialog = new JFileChooser();
				assignDialog.setFileFilter(new FileNameExtensionFilter("XML File", "xml"));
				assignDialog.setAcceptAllFileFilterUsed(false);
			    int returnValue = assignDialog.showDialog(getParent(), "Assign Test");
			    if (returnValue == JFileChooser.APPROVE_OPTION){
			    	TestLoader loader = new TestLoader();
			    	TestSaver saver = new TestSaver();
			    	saver.setFile("subjects/" + comboBox.getSelectedItem() + "/" + assignDialog.getSelectedFile().getName());
			    	try {
						saver.save(loader.loadTest(assignDialog.getSelectedFile().getAbsolutePath()));
						String subject = (String) comboBox.getSelectedItem();
						File subjectDir = new File ("subjects/" + subject);
						File[] tests = subjectDir.listFiles();
						Vector<String> testList = new Vector<String>();
						for (int i = 0; i < tests.length; i++){
							String fileName = tests[i].getName();
							if (fileName.substring(fileName.indexOf('.')).equals(".xml"))
							   testList.add(fileName);
						}
						list.setListData(testList);
					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (ParserConfigurationException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (TransformerException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (SAXException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
			    }
			}
		});
		btnAssignTest.setBounds(10, 302, 116, 23);
		contentPanel.add(btnAssignTest);
		
		final JButton btnRemoveCurrentSubject = new JButton("Remove Current Subject");
		btnRemoveCurrentSubject.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if (btnRemoveCurrentSubject.isEnabled()){
					File subject = new File ("subjects/" + comboBox.getSelectedItem());
					File[] subTests = subject.listFiles();
					for (int i = 0; i < subTests.length; i++){
						subTests[i].delete();
					}
					subject.delete();
					comboBox.removeItemAt(comboBox.getSelectedIndex());
					if (comboBox.getItemCount() == 0){
						btnRemoveCurrentSubject.setEnabled(false);
					}
				}
			}
		});
		btnRemoveCurrentSubject.setFont(new Font("Tahoma", Font.PLAIN, 10));
		btnRemoveCurrentSubject.setBounds(129, 60, 149, 23);
		contentPanel.add(btnRemoveCurrentSubject);
		
		comboBox.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				String subject = (String) comboBox.getSelectedItem();
				File subjectDir = new File ("subjects/" + subject);
				File[] tests = subjectDir.listFiles();
				if (tests != null){
					Vector<String> testList = new Vector<String>();
					for (int i = 0; i < tests.length; i++){
						String fileName = tests[i].getName();
						if (fileName.substring(fileName.indexOf('.')).equals(".xml"))
						   testList.add(fileName);
					}
				   list.setListData(testList);
				}
				if (comboBox.getItemCount() > 0){
					btnRemoveCurrentSubject.setEnabled(true);
				}
			}
		});
		
		if (comboBox.getItemCount() > 0){
			String subject = (String) comboBox.getSelectedItem();
			File subjectDir = new File ("subjects/" + subject);
			File[] tests = subjectDir.listFiles();
			if (tests != null){
				Vector<String> testList = new Vector<String>();
				for (int i = 0; i < tests.length; i++){
					String fileName = tests[i].getName();
					if (fileName.substring(fileName.indexOf('.')).equals(".xml"))
					   testList.add(fileName);
				}
			   list.setListData(testList);
			}
		} else {
			btnRemoveCurrentSubject.setEnabled(false);
		}
		
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton closeButton = new JButton("Close");
				closeButton.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent arg0) {
						dispose();
					}
				});
				closeButton.setActionCommand("");
				buttonPane.add(closeButton);
			}
		}
		
		setVisible (true);
	}
}
